package yuejian_07.yuejian_07.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.javabean.Train;
import yuejian_07.yuejian_07.model.TrainModel;


public class TrainInfoActivity extends Activity implements RetrofitListener<Train> {
    TextView t_name,address,date,price,man_count,phone,intro;
    ImageView img_return,img_collect,pic;
    Button enroll;
    //private int user_id;
    private int train_id;
    private String train_name;
    private int train_price;
    private String train_pic;
    private String train_intro;
    private String train_address;
    private String train_date;
    private int train_phone;
    private Train train;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.train_01_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        t_name=(TextView)findViewById(R.id.train_name);
        address=(TextView)findViewById(R.id.address);
        date=(TextView)findViewById(R.id.date);
        price=(TextView)findViewById(R.id.price);
        man_count=(TextView)findViewById(R.id.man_count);
        phone=(TextView)findViewById(R.id.phone);
        intro=(TextView)findViewById(R.id.info);
        img_return=(ImageView) findViewById(R.id.img_return);
        img_collect=(ImageView) findViewById(R.id.collect);
        pic=(ImageView) findViewById(R.id.pic);
        enroll=(Button) findViewById(R.id.enroll);
    }

    public void initEvents(){

        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        img_collect.setOnClickListener(new View.OnClickListener() {//收藏
            @Override
            public void onClick(View v) {
                //collectActivity();
            }
        });

        enroll.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainInfoActivity.this, EnrollTrainActivity.class);
                Bundle bundle  = new Bundle();
                bundle.putString("train_name",train_name);
                bundle.putInt("train_price",train_price);
                bundle.putInt("train_id",train_id);
                //bundle.putInt("user_id",user_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    public void initData(){

        getActivityInfo();
        //getUserId();
        //checkCollect();

    }

    //获取菜品信息数据
    public void getActivityInfo(){

        Bundle bundle = getIntent().getExtras();
        train_id = bundle.getInt("train_id");
        TrainModel trainModel = new TrainModel();
        trainModel.getTrainById(train_id,this);

    }
    @Override
    public void onSuccess(Train train){
        train_name = train.getTrain_name();
        train_price = train.getTrain_price();
        train_pic ="http://10.0.2.2/neu1_yuejian/Public/Admin/photo/"+train.getTrain_pic();
        train_intro=train.getTrain_intro();
        train_address=train.getTrain_address();
        train_date=train.getTrain_time();
        train_phone=train.getTrain_phone();
        t_name.setText(train_name);
        price.setText("￥" + train_price);
        intro.setText(train_intro);
        address.setText(train_address);
        date.setText(train_date);
        phone.setText(train_phone+"");
        if (train_pic != "") {
            Picasso.with(TrainInfoActivity.this)
                    .load(train_pic)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            pic.setImageResource(R.drawable.no_pic);
        }

    }
    @Override
    public void onFail(){ Toast.makeText(TrainInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

    //获取个人用户信息
    /*public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
    }*/

}
